<template>
	<view class="container">
		<!-- <u-navbar title="个人信息" placeholder :border-bottom="false"></u-navbar> -->
		<view v-if="data.dataList && data.dataList.basic && data.dataList.contact && data.utype == 2">
			<u-cell-group>
				<u-cell-item title="头像" class="required" icon-size="24" :arrow="false" :title-style="data.titleStyle">
					<u-avatar :src="data.dataList.basic.photo_img_src" @click="data.sheetShow = true"></u-avatar>
				</u-cell-item>
				<u-cell-item required isLink title="姓名" :title-style="data.titleStyle" :use-label-slot="true"
					:arrow="false">
					<template v-slot:label>
						<u-field :border-bottom="false" label-width="0"
							:field-style="data.dataList.basic.fullname ? data.labelStyle:data.labelStyle2"
							v-model="data.dataList.basic.fullname" placeholder="请输入姓名"
							:placeholder-style="data.placeholderStyle"  />
					</template>
				</u-cell-item>
				<u-cell-item required isLink @click="data.isSex= true" :title-style="data.titleStyle"
					:label-style="data.dataList.basic.sex != 0 ? data.labelStyle:data.labelStyle2" title="性别"
					:label="data.dataList.basic.sex != 0 ? data.dataList.basic.sex_text : '请选择性别'">
				</u-cell-item>
				<u-cell-item isLink @click="data.isBirthday = true" title="出生日期" :title-style="data.titleStyle"
					:label="data.dataList.basic.birthday ? $u.timeFormat(new Date(data.dataList.basic.birthday), 'yyyy-mm') : '请选择出生年月'"
					:label-style="data.dataList.basic.birthday  ? data.labelStyle:data.labelStyle2">
				</u-cell-item>
				<u-cell-item isLink title="" :title-style="data.titleStyle" :use-label-slot="true" :arrow="false">

					<template v-slot:label>
						<view style="display: flex;justify-content:space-between;">
							<input disabled @click="data.isWork = true" type="text" :style="data.labelStyle"
								:value="data.dataList.basic.enter_job_time?$u.timeFormat(new Date(data.dataList.basic.enter_job_time), 'yyyy-mm') : data.dateshow"
								placeholder="请选择" :placeholder-style="data.placeholderStyle">
							<u-icon name="arrow-right" @click="data.isWork = true"></u-icon>
						</view>
					</template>
					<template v-slot:title>
						<view class="titleleft">
							<text>开始工作时间</text>
							<u-checkbox shape="circle" @change="enterjobtime" v-model="data.thisyear"><text
									style="white-space: nowrap !important;">应届生/无经验</text></u-checkbox>
						</view>
					</template>
				</u-cell-item>
				<u-cell-item isLink @click="data.show = true" title="最高学历" :title-style="data.titleStyle"
					:label="data.dataList.basic.education_text || '请选择最高学历'"
					:label-style="data.dataList.basic.education_text  ? data.labelStyle:data.labelStyle2">
				</u-cell-item>
				<u-cell-item required isLink title="联系电话" :title-style="data.titleStyle" :use-label-slot="true"
					:arrow="false">
					<template v-slot:label>
						<u-field :border-bottom="false" label-width="0"
							:field-style="data.dataList.contact.mobile ? data.labelStyle:data.labelStyle2"
							v-model="data.dataList.contact.mobile" placeholder="请输入联系电话"
							:placeholder-style="data.placeholderStyle"  />
					</template>
				</u-cell-item>
				<u-cell-item required isLink title="" :title-style="data.titleStyle" :use-label-slot="true"
					:arrow="false">
					<template v-slot:label>
						<view style="display: flex;">
							<u-field :border-bottom="false" label-width="0" @input="changeInput"
								:field-style="data.dataList.contact.weixin ? data.labelStyle:data.labelStyle2"
								v-model="data.dataList.contact.weixin" placeholder="请输入微信号"
								:placeholder-style="data.placeholderStyle"  />
						</view>
					</template>
					<template v-slot:title>
						<view class="titleleft"  @click.stop>
							<text>微信号</text>
							<u-checkbox  @change="checkboxChange" shape="circle" v-model="data.phoneNum"><text
									style="white-space: nowrap !important;">同手机号</text></u-checkbox>
						</view>
					</template>
				</u-cell-item>
				<u-cell-item isLink title="QQ" :title-style="data.titleStyle" :use-label-slot="true" :arrow="false">
					<template v-slot:label>
						<u-field :border-bottom="false" label-width="0"
							:field-style="data.dataList.contact.qq ? data.labelStyle:data.labelStyle2"
							v-model="data.dataList.contact.qq" placeholder="请输入QQ"
							:placeholder-style="data.placeholderStyle"  />
					</template>
				</u-cell-item>

				<u-cell-item isLink @click="data.ismarriage = true" title="婚姻状态" :title-style="data.titleStyle"
					:label="data.dataList.basic.marriage_text || '请选择婚姻状态'"
					:label-style="data.dataList.basic.marriage_text ? data.labelStyle:data.labelStyle2">
				</u-cell-item>
				<u-cell-item isLink title="现居住地" :title-style="data.titleStyle" :use-label-slot="true" :arrow="false">
					<template v-slot:label>
						<u-field :border-bottom="false" label-width="0"
							:field-style="data.dataList.basic.residence ? data.labelStyle:data.labelStyle2"
							v-model="data.dataList.basic.residence" placeholder="请输入地址"
							:placeholder-style="data.placeholderStyle"  />
					</template>
				</u-cell-item>

			</u-cell-group>
			<view class="more" @tap="data.showmore = !data.showmore">
				<text>完善信息，获得更多求职机会</text>
				<image
					:src=" data.showmore? '/pages/pagesPersonal/static/personal/down.png':'/pages/pagesPersonal/static/personal/up.png'"
					mode=""></image>
			</view>
			<u-cell-group v-if="data.showmore">
				<u-cell-item isLink @click="data.ismajor=true" title="专业" :title-style="data.titleStyle"
					:label="data.dataList.basic.major_text || '请填写'"
					:label-style="data.dataList.basic.major_text ? data.labelStyle:data.labelStyle2">
				</u-cell-item>
				<u-cell-item isLink title="身高(cm)" :title-style="data.titleStyle" :use-label-slot="true" :arrow="false">
					<template v-slot:label>
						<u-field :border-bottom="false" label-width="0" type="number"
							ld-style="data.dataList.basic.height != 0 ? data.labelStyle:data.labelStyle2"
							v-model="data.dataList.basic.height" placeholder="请输入身高"
							:placeholder-style="data.placeholderStyle"  />
					</template>
				</u-cell-item>
				<u-cell-item isLink title="籍贯" :title-style="data.titleStyle" :use-label-slot="true" :arrow="false">
					<template v-slot:label>
						<u-field :border-bottom="false" label-width="0"
							:field-style="data.dataList.basic.householdaddress? data.labelStyle:data.labelStyle2"
							v-model="data.dataList.basic.householdaddress" placeholder="请填写"
							:placeholder-style="data.placeholderStyle"  />
					</template>
				</u-cell-item>
				<u-cell-item isLink title="邮箱" :title-style="data.titleStyle" :use-label-slot="true" :arrow="false">
					<template v-slot:label>
						<u-field :border-bottom="false" label-width="0"
							:field-style="data.dataList.contact.email? data.labelStyle:data.labelStyle2"
							v-model="data.dataList.contact.email" placeholder="请填写"
							:placeholder-style="data.placeholderStyle"  />
					</template>
				</u-cell-item>

				<u-cell-item isLink title="身份证" :title-style="data.titleStyle" :use-label-slot="true" :arrow="false">
					<template v-slot:label>
						<u-field :border-bottom="false" label-width="0"
							:field-style="data.dataList.basic.idcard? data.labelStyle:data.labelStyle2"
							v-model="data.dataList.basic.idcard" placeholder="请填写"
							:placeholder-style="data.placeholderStyle"  />
					</template>
				</u-cell-item>
			</u-cell-group>
		</view>
		<u-action-sheet :list="data.sheetList" cancelText="取消" v-model="data.sheetShow" safeAreaInsetBottom
			@click="sheetClick">
		</u-action-sheet>
		<!-- //性别 -->
		<u-picker v-model="data.isSex" :range="data.sexList" mode="selector" 
			@confirm="sexConfirm" :default-selector="data.sexIndex"></u-picker>
		<!-- //出生日期 -->
		<u-picker v-model="data.isBirthday" :params="data.params" @confirm="birthdayConfirm"
			:default-time="$u.timeFormat(new Date(data.dataList.basic.birthday), 'yyyy-mm-dd')" mode="time"
			></u-picker>
		<!-- //开始工作时间 -->
		<u-picker v-model="data.isWork" :params="data.params" @confirm="workConfirm"
			:default-time="data.dataList.basic.enter_job_time?$u.timeFormat(new Date(data.dataList.basic.enter_job_time), 'yyyy-mm-dd'):$u.timeFormat(new Date(), 'yyyy-mm-dd')"
			mode="time" ></u-picker>
		<!-- 学历 -->
		<u-picker v-model="data.show" @confirm="confirm" range-key="name" mode="selector" :range="data.columns"
			:default-selector="data.defaultIndex" ></u-picker>
		<!-- 婚姻状况 -->
		<u-picker v-model="data.ismarriage" @confirm="confirmMarriage" range-key="name" mode="selector"
			:range="data.marriagelist" :default-selector="data.marriagechange" ></u-picker>
		<!-- 专业 -->
		<u-select v-model="data.ismajor" mode="mutil-column-auto" :list="data.majorLsit"
			@confirm="categoryConfirm"></u-select>

		<view class="btnBox">
			<button @tap="$u.throttle(toSave(code),2000)">生成简历</button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		toRaw
	} from "vue"
	import {
		onReady,
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	const data = ref({
		params: {
			year: true,
			month: true,
			day: false,
			hour: false,
			minute: false,
			second: false
		},
		phoneNum: false,
		sheetShow: false,
		ismarriage: false, //婚姻状况
		ismajor: false,
		marriagelist: [
			"保密",
			"未婚",
			"已婚"
		],
		utype: uni.getStorageSync('utype'),
		dataList: {
			basic: {
				fullname: "",
				sex: 0
			},
			contact: {}

		},
		columns: [],
		defaultIndex: [],
		marriagechange: [],
		sexList: [
			'男', '女'
		],
		sexIndex: [],
		isBirthday: false,
		show: false,
		titleStyle: {
			'font-size': '30rpx',
			'color': '#303133'
		},
		labelStyle: {
			'font-size': '30rpx',
			'color': '#000'
		},
		labelStyle2: {
			'font-size': '28rpx',
			'color': '#c0c4cc'
		},
		placeholderStyle: "font-size: 28rpx;color:#c0c4cc",
		isWork: false,
		thisyear: false,
		showmore: false,
		sheetList: [{
			text: '拍照',
			index: 0
		}, {
			text: '从相册中选择',
			index: 1
		}],
		majorLsit: [],
		dateshow:''

	})

	const init = async () => {
		if (data.value.utype == 1) {
			//企业端
		} else if (data.value.utype == 2) {
			//求职端
			await getEducationList()
			await getClassifyInfo()
			await getUserInfo()
		}
	}
	// 获取求职者个人信息
	const getUserInfo = async () => {
		let res = await uni.$u.api.getOnlineMange()
		if (res) {
			data.value.dataList = res
			let basic = data.value.dataList.basic
			let cur = data.value.columns.findIndex(item => {
				return item.name == basic.education_text
			})
			if (cur != -1) {
				data.value.defaultIndex = [cur]
			}

			let sexCur = basic.sex == 0 ? 0 : data.value.sexList.findIndex(item => {
				return item == basic.sex_text
			})
			data.value.sexIndex.push(sexCur)

			if (data.value.dataList.basic.enter_job_time == 0) {
				data.value.dataList.basic.enter_job_time = ""
				data.value.dateshow = data.value.dateshow = uni.$u.timeFormat(new Date(), 'yyyy-mm')
				data.value.thisyear = true
			} else if (data.value.dataList.basic.enter_job_time.toString().length == 10) {
				data.value.dataList.basic.enter_job_time = uni.$u.timeFormat(new Date(data.value.dataList.basic
					.enter_job_time * 1000), 'yyyy-mm')
			} else {
				data.value.dataList.basic.enter_job_time = uni.$u.timeFormat(new Date(data.value.dataList.basic
					.enter_job_time), 'yyyy-mm')
			}

			if (data.value.dataList.basic.height == 0) {
				data.value.dataList.basic.height = ""
			}
			let marrianum = data.value.marriagelist.findIndex(item => {
				return item == basic.marriage_text
			})
			
			if(data.value.dataList.contact.weixin == data.value.dataList.contact.mobile){
				data.value.phoneNum = true
			}

			data.value.marriagechange.push(marrianum)

		}
	}
	// 获取学历列表
	const getEducationList = async () => {
		let params = {
			type: 'education'
		}
		let res = await uni.$u.api.getClassifyInfo({
			params
		})
		if (res) {
			data.value.columns = res
		}

	}
	// 点击头像索引项
	const sheetClick = async (index) => {
		if (index == 0) {
			uni.chooseImage({
				count: 1, //默认9
				sizeType: ['original'],
				sourceType: ['camera'], //从相机选择
				success: async (option) => {
					let url = option.tempFilePaths[0]
					let params = {
						filePath: url,
						name: 'file'
					}
					let res = await uni.$u.api.getUploadSave(params)
					if (res) {
						if (uni.getStorageSync('utype') == 1) {

						} else {
							await uni.$u.api.uploadPhoto(params)
							uni.$u.toast('上传成功')
							data.value.dataList.basic.photo_img_src = res.file_url
							data.value.dataList.basic.photo_img = res.file_id
						}
					}
				},
				fail: (err) => {}

			})


		} else {
			uni.chooseImage({
				count: 1, //默认9
				sizeType: ['original'],
				sourceType: ['album'], //从相册选择
				success: async (option) => {
					let url = option.tempFilePaths[0]
					let params = {
						filePath: url,
						name: 'file'
					}
					let res = await uni.$u.api.getUploadSave(params)
					if (res) {
						if (uni.getStorageSync('utype') == 1) {

						} else {
							await uni.$u.api.uploadPhoto(params)
							uni.$u.toast('上传成功')
							data.value.dataList.basic.photo_img_src = res.file_url
							data.value.dataList.basic.photo_img = res.file_id
						}
					}
				}
			})
		}
	}
	// 修改性别
	const sexConfirm = (e) => {
		let val = e[0] == '0' ? 1 : 2
		data.value.dataList.basic.sex = val
		data.value.dataList.basic.sex_text = val == 1 ? "男" : "女"
	}
	// 修改生日
	const birthdayConfirm = (e) => {
		data.value.dataList.basic.birthday = e.year + '-' + e.month
	}
	// 修改工作时间
	const workConfirm = (e) => {

		let newdate = uni.$u.timeFormat(new Date(), 'yyyy-mm')
		let date = e.year + '-' + e.month
		if (newdate == date) {
			data.value.thisyear = true
			data.value.dataList.basic.enter_job_time = ""
		} else {
			data.value.thisyear = false
			data.value.dataList.basic.enter_job_time = e.year + '-' + e.month
		}

	}

	const checkboxChange = (e) => {
		if (e.value) {
			data.value.dataList.contact.weixin = data.value.dataList.contact.mobile
		}

	}
	const enterjobtime = (e) => {
		if (e.value) {
			data.value.dataList.basic.enter_job_time = ""
			data.value.dateshow = data.value.dateshow = uni.$u.timeFormat(new Date(), 'yyyy-mm')
			
		}else{
			data.value.dateshow = data.value.dateshow = ""
		}
	}
	const changeInput = (e) => {
		if (e === data.value.dataList.contact.mobile && data.value.dataList.contact.mobile) {
			data.value.phoneNum = true

		} else {
			data.value.phoneNum = false
		}
	}

	// 修改最高学历
	const confirm = (e) => {

		let index = e[0]
		data.value.dataList.basic.education_text = data.value.columns[index].name;
		data.value.dataList.basic.education = data.value.columns[index].id;
	}
	// 婚姻状态
	const confirmMarriage = (e) => {
		let index = e[0]
		data.value.dataList.basic.marriage_text = data.value.marriagelist[index]
		data.value.dataList.basic.marriage = index
	}

	// 专业
	const categoryConfirm = (e) => {
		data.value.dataList.basic.major_text = e[1].label
		data.value.dataList.basic.major1 = e[0].value
		data.value.dataList.basic.major2 = e[1].value
	}
	// 获取字典表信息
	const getClassifyInfo = async () => {
		let params = {
			type: 'major'
		}
		let res = await uni.$u.api.getClassifyInfo({
			params
		})
		if (res) {
			data.value.majorLsit = res
		}
	}

	const toSave = async () => {

		if (!data.value.dataList.basic.fullname) {
			uni.$u.toast('请填写姓名')
		} else if (!data.value.dataList.contact.mobile) {
			uni.$u.toast('请填联系电话')
		} else if (!uni.$u.test.mobile(data.value.dataList.contact.mobile)) {
			uni.$u.toast('请填写正确的联系电话')
		} else if (!data.value.dataList.contact.weixin) {
			uni.$u.toast('请填写微信号')
		} else if (!uni.$u.test.wx(data.value.dataList.contact.weixin) && !uni.$u.test.mobile(data.value.dataList
				.contact.weixin)) {
			uni.$u.toast('请填写正确的微信号')

		} else if (data.value.dataList.contact.qq && !uni.$u.test.qq(data.value.dataList.contact.qq)) {
			uni.$u.toast('请填写正确的QQ号')
		} else if (data.value.dataList.contact.email && !uni.$u.test.email(data.value.dataList.contact.email)) {
			uni.$u.toast('请填写正确的邮箱号')
		} else if (data.value.dataList.basic.idcard && !uni.$u.test.idCard(data.value.dataList.basic.idcard)) {
			uni.$u.toast('请填写正确的身份证')
		} else {
			let params = {
				basic: data.value.dataList.basic,
				contact: data.value.dataList.contact
			}
			let res = await uni.$u.api.updateUserInfo(
				params
			)
			if (res) {
				uni.$u.toast('保存成功')
				setTimeout(() => {
					uni.$u.route({
						type: 'back'
					})
				}, 1000)
			}
		}






	}
	onShow(() => {
		init()
	})
</script>

<style lang="scss" scoped>
	page,
	body {
		width: 100vw;
		height: 100vh;
		background-color: #fff;
	}

	.more {

		width: 100%;
		height: 100rpx;
		display: flex;
		justify-content: center;
		align-items: center;

		image {
			margin-left: 20rpx;
			width: 40rpx;
			height: 40rpx;
		}
	}

	:deep .u-flex-1 {
		margin-left: 0 !important;
	}

	.btnBox {
		width: 100vw;
		height: 120rpx;
		display: flex;
		align-items: center;
		justify-content: center;

		button {
			width: 684rpx;
			background-color: #00d075;
			color: #fff;
			height: 68rpx;
			line-height: 68rpx;
			font-size: 28rpx;
			text-align: center;
			font-family: Arial;

			&::after {
				border: none;
			}

		}

	}

	.titleleft {
		width: calc(100vw - 64rpx);
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 100rpx;
	}

	:deep .u-cell--required{
		&::before{
			top: 24px!important;
		}
		
	}
	:deep .u-field__input-wrap {
		width: 500rpx !important;
	}
	:deep .u-cell{
		height: 200rpx;
	}
	
</style>